package net.gyapp.markim.module.basic;

import android.Manifest;
import android.content.Intent;
import android.text.TextUtils;

import com.easyder.wrapper.core.manager.ImageManager;
import com.easyder.wrapper.core.scheduler.TaskScheduler;
import com.easyder.wrapper.utils.LogUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.luck.picture.lib.permissions.RxPermissions;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;

import net.gyapp.markim.R;
import net.gyapp.markim.WrapperApplication;
import net.gyapp.markim.core.ImManager;
import net.gyapp.markim.module.basic.base.WrapperBaseActivity;
import net.gyapp.markim.module.mine.account.LoginActivity;
import net.gyapp.markim.utils.GlideScaleBottomTransform;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.winds.widget.autolayout.utils.AutoUtils;

/**
 * Author:  winds
 * Data:    2018/7/6
 * Version: 1.0
 * Desc:
 */


public class SplashActivity extends WrapperBaseActivity {
    @Override
    protected void initView(Intent intent) {
        ImmersionBar.with(this).transparentBar().init();
        GlideScaleBottomTransform transform = new GlideScaleBottomTransform(mActivity, AutoUtils.getScreenHeight() / AutoUtils.getScreenWidth());
        ImageManager.load(mActivity, findViewById(R.id.image), R.drawable.ic_splash, transform);

        getPermission();
    }


    @Override
    protected int getViewLayout() {
        return R.layout.activity_splash;
    }


    private void getPermission() {
        new RxPermissions(mActivity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (!aBoolean) {
                            showToast("权限被拒绝");
                        }
                        onPermissionGain(aBoolean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("申请权限出错");
                        onPermissionGain(false);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void onPermissionGain(Boolean aBoolean) {
        TaskScheduler.getMainHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                disposeUserStatus();
            }
        }, 500);
    }

    private void enterHome() {
        startActivity(MainActivity.getIntent(mActivity));
        finish();
        overridePendingTransition(0, 0);
    }

    private void enterLogin() {
        startActivity(LoginActivity.getIntent(mActivity, false, false));
        finish();
        overridePendingTransition(0, 0);
    }

    private void disposeUserStatus() {
        if (WrapperApplication.isLogin()) {
            String account = Helper.getAccount();
            String cipher = Helper.getCipher();
            if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(cipher)) {
                processToken(Helper.getToken());
                if (!ImManager.getInstance().isConnect()) {
                    ImManager.getInstance().connect();
                }
                ImManager.getInstance().login(account, cipher);
                LogUtils.i(" -- Socket -- ", "发起登陆连接");
                enterHome();
            } else {
                enterLogin();
            }
        } else {
            enterLogin();
        }
    }


    private void processToken(String token) {
        HttpHeaders headers = OkGo.getInstance().getCommonHeaders();
        if (headers != null) {
            headers.clear();
        } else {
            headers = new HttpHeaders();
        }
        headers.put("Authorization", token);
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        OkGo.getInstance().addCommonHeaders(headers);

    }
}
